﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BrokerBase.Lib.Client;
using System.Management;

namespace Broker.Comm
{
    public static class ClientUtil
    {
        private static string diskId;
        private static string macId;
        private static string cpuId;
        private static string mbid;

        public static TerminalInfo CreateTerminalInfo()
        {
            TerminalInfo info = new TerminalInfo();
            info.ClientTime = DateTime.Now;
            info.DISKSerialNo = GetDiskId();
            info.MACSerialNo = GetMacId();
            info.MBSerialNo = GetMotherBoardId();
            info.OSVersion = Environment.OSVersion.VersionString;

            return info;

        }

        public static string GetCpuId()
        {
            ManagementClass mc = new ManagementClass("Win32_Processor");
            ManagementObjectCollection moc = mc.GetInstances();

            ManagementObject[] mos = new ManagementObject[1];

            moc.CopyTo(mos, 0);
            object o = mos[0].Properties["ProcessorId"].Value;

            mos[0].Properties["ProcessorId"].Value.ToString();
            
            string tmp = o == null ? "CP12345N" : o.ToString();

            return tmp;
        }

        public static string GetDiskId()
        {
           
            try
            {
                if (string.IsNullOrEmpty(diskId))
                {
                    ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");

                    foreach (ManagementObject mo in searcher.Get())
                    {
                        object v = mo["SerialNumber"];
                        diskId = v == null ? "DK12345678N" : v.ToString();
                        break;
                    }
                }
                
                return diskId;
            }
            catch (Exception er)
            {
                AppGlobal.Log.Error(er.Message);
                AppGlobal.Log.Error(er.StackTrace);
                throw er;
            }

        }

        public static string GetMacId()
        {
            if (string.IsNullOrEmpty(macId))
            {
                ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
                ManagementObjectCollection moc = mc.GetInstances();

                foreach (ManagementObject mo in moc)
                {
                    if ((bool)mo["IPEnabled"] == true)
                    {
                        object o = mo["MacAddress"];
                        macId = o == null ? "MC12345N" : o.ToString();
                        break;
                    }
                }
            }
            return macId;
        }

        public static string GetMotherBoardId()
        {
            if (string.IsNullOrEmpty(mbid))
            {

                ManagementObjectSearcher mc = new ManagementObjectSearcher("Select SerialNumber From Win32_BIOS");

                foreach (ManagementObject mo in mc.Get())
                {
                    object o = mo["SerialNumber"];

                    mbid = o==null?"MB12345N":o.ToString();
                    break;
                }
            }
            return mbid;
        }

        public static string Evdb()
        {
            string tmp = "F" + En("xml777");
            return tmp;
        }

        public static string En(string name)
        {
            return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(name, "md5");
        }
    }
}
